/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function Pipe(data) {
    this.image = new Image();
    this.image.src = data.imgSrc;
    this.mapWidth = data.mapWidth;
    this.frameWidth = data.frameWidth;
}
Pipe.prototype = {
    draw: function(context, locationCutY, cx, cy, framePipe, frameHeight) {
        context.drawImage(this.image, framePipe * this.frameWidth, locationCutY, this.frameWidth, frameHeight,
                cx, cy, this.frameWidth, frameHeight);
    },
}
function PipePair(data) {
    this.pipeTop = new Pipe({
        imgSrc: "ImagesGame/pipesTop.png",
        mapWidth: data.mapWidth,
        mapHeight: data.mapHeight,
        frameWidth: data.frameWidth,
        frameHeight: 200,
    });
    this.pipeButtom = new Pipe({
        imgSrc: "ImagesGame/pipesButtom.png",
        mapWidth: data.mapWidth,
        mapHeight: data.mapHeight,
        frameWidth: data.frameWidth,
        frameHeight: 200,
    });
}
function PipeList(data) {
    this.mapWidth = data.mapWidth;
    this.mapHeight = data.mapHeight;
    this.frameWidth = data.frameWidth;
    this.speedX = data.speedX;
    this.cyTop = data.cyTop;
    this.cyButtom = data.cyButtom;
    this.list = [];
    this.numPipe = 5;
    this.distanceXPipe = Math.floor(this.mapWidth / (this.numPipe - 1));
    this.distanceYPipe = 140;
    this.minHeight = 50;
    this.pipePair = new PipePair({
        mapWidth: this.mapWidth,
        mapHeight: this.mapHeight,
        frameWidth: this.frameWidth,
    });
    this.startX = data.startX;
    for (var i = 0; i < this.numPipe; i++)
        this.list.push({
            heightTop: 0,
            frame: 0,
            score: false,
        });
}
PipeList.prototype = {
    gameReddy: function() {
        for (var i = 0; i < this.numPipe; i++)
        {
            this.list[i].heightTop = Math.floor(Math.random() * (530 - this.distanceYPipe - 2 * this.minHeight) + this.minHeight);
            this.list[i].frame = 3;
            this.list[i].score = false;
        }
        this.list[0].cx = this.startX;
        this.list[0].heightButtom = 530 - this.distanceYPipe - this.list[0].heightTop;
        for (var i = 1; i < this.numPipe; i++)
        {
            this.list[i].cx = this.list[i - 1].cx + this.distanceXPipe;
            this.list[i].heightButtom = 530 - this.distanceYPipe - this.list[i].heightTop;
        }
        this.list[this.numPipe - 1].frame = Math.floor(Math.random() * 3);
    },
    draw: function(context) {
        for (var i = 0; i < this.numPipe; i++)
        {
            this.pipePair.pipeTop.draw(context, 665 - this.list[i].heightTop, this.list[i].cx, this.cyTop, this.list[i].frame, this.list[i].heightTop);
            this.pipePair.pipeButtom.draw(context, 0, this.list[i].cx, this.mapHeight - this.cyButtom - this.list[i].heightButtom, this.list[i].frame, this.list[i].heightButtom);
        }
    },
    update: function() {
        this.move();
    },
    move: function() {
        for (var i = 0; i < this.numPipe; i++)
        {
            this.list[i].cx -= this.speedX;
            if (this.list[i].cx + this.frameWidth < 0) {
                var indexPrevious = i - 1;
                if (indexPrevious < 0) {
                    indexPrevious = this.numPipe - 1;
                }
                if (i == 4)
                    this.list[i].frame = Math.floor(Math.random() * 3);
                this.list[i].cx = this.list[indexPrevious].cx + this.distanceXPipe;
                this.list[i].heightTop = Math.floor(Math.random() * (530 - this.distanceYPipe - 2 * this.minHeight) + this.minHeight);
                this.list[i].heightButtom = 530 - this.distanceYPipe - this.list[i].heightTop;
                this.list[i].score = false;
            }
        }
    },
}

